package com.sso.server.service.impl;

import com.sso.core.store.SsoLoginStore;
import com.sso.server.core.model.LoginPasswordType;
import com.sso.server.core.model.UserInfo;
import com.sso.server.service.UserService;
import com.sso.server.core.result.ReturnData;
import com.sso.server.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    private static List<UserInfo> mockUserList = new ArrayList<>(1);
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Value("${sso.auth.jdbc.query.tableName}")
    public String authTableName;

    @Value("${sso.auth.jdbc.query.field.user}")
    public String authFieldUser;

    @Value("${sso.auth.jdbc.query.field.password}")
    public String authFieldPassword;

    @Value("${sso.auth.jdbc.query.passwordType}")
    public String authFieldPasswordType;


    @Override
    public ReturnData<UserInfo> findUser(String username, String password) {

        if (mockUserList.size() == 0) {
            initUserInfo();
        }

        if (username == null || username.trim().length() == 0) {
            return new ReturnData<UserInfo>(ReturnData.FAIL_CODE, "请输入用户名称.");
        }
        if (password == null || password.trim().length() == 0) {
            return new ReturnData<UserInfo>(ReturnData.FAIL_CODE, "请输入用户密码.");
        }

        for (UserInfo mockUser : mockUserList) {
            if (mockUser.getUsername().equals(username)) {

                String pass = mockUser.getPassword();
                if (LoginPasswordType.MD5.getValue().equals(authFieldPasswordType)) {
                    pass = MD5Utils.MD5Upper(password);
                }
                if (LoginPasswordType.md5.getValue().equals(authFieldPasswordType)) {
                    pass = MD5Utils.MD5Lower(password);
                }
                if (mockUser.getPassword().equals(pass)) {
                    return new ReturnData<UserInfo>(mockUser);
                } else {
                    return new ReturnData<UserInfo>(ReturnData.FAIL_CODE, "用户名称或密码错误.");
                }

            }
        }

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                initUserInfo();

            }
        });
        thread.start();
        return new ReturnData<UserInfo>(ReturnData.FAIL_CODE, "用户名称或密码错误.");
    }

    @Override
    public ReturnData<UserInfo> findUserByMobile(String mobile) {

        if (mockUserList.size() == 0) {
            initUserInfo();
        }

        for (UserInfo mockUser : mockUserList) {
            if (mockUser.getUsername().equals(mobile)) {
                return new ReturnData<UserInfo>(mockUser);
            }
        }

        return null;
    }

    /**
     * loadUserInfo
     */
    public void initUserInfo() {

        List<Map<String, Object>> maps = jdbcTemplate.queryForList("SELECT id,".concat(authFieldUser).concat(",").concat(authFieldPassword).concat(" FROM ").concat(authTableName));
        mockUserList.clear();
        mockUserList = new ArrayList(maps.size() + 1);
        maps.forEach(map -> {
            String account = (String) map.get(authFieldUser);
            String password = (String) map.get(authFieldPassword);
            UserInfo userInfo = new UserInfo();
            userInfo.setUsername(account);
            userInfo.setPassword(password);
            userInfo.setId(String.valueOf(map.get("id")));
            mockUserList.add(userInfo);
        });
        System.out.println("init user count > " + mockUserList.size());

    }


}
